Hard disks with capacities measured in the hundreds
of gigabytes are commonplace even in low-end systems nowadays, so disk
space is much less of a problem than it used to be. Still, you need to
keep track of how much free space you have on your disk drives,
particularly the %SystemDrive% (usually the C: drive), which usually stores the virtual memory page file.
One
way to check disk free space is to view the Computer folder using
either the Tiles or Content views, which include the free space and
total disk space with each drive icon, or the Details view, which
includes columns for Total Size and Free Space, as shown in Figure 1.
Alternatively, right-click the drive in Windows Explorer and then click
Properties. The disk’s total capacity, as well as its current used and
free space, appear in the General tab of the disk’s property sheet.
Tip
To see the File System and Percent Full columns shown in Figure 1, right-click any column header and then click the File System property, then repeat and click the Percent Full property.
Listing 7.1 presents a VBScript procedure that displays the status and free space for each drive on your system.
Listing 1. A VBScript Example That Displays the Status and Free Space for Your System’s Drives
Option Explicit Dim objFSO, colDiskDrives, objDiskDrive, strMessage
' Create the File System Object Set objFSO = CreateObject("Scripting.FileSystemObject")
' Get the collection of disk drives Set colDiskDrives = objFSO.Drives
' Run through the collection strMessage = "Disk Drive Status Report" & vbCrLf & vbCrLf For Each objDiskDrive in colDiskDrives
' Add the drive letter to the message strMessage = strMessage & "Drive: " & objDiskDrive.DriveLetter & vbCrLf ' Check the drive status If objDiskDrive.IsReady = True Then
' If it's ready, add the status and the free space to the message strMessage = strMessage & "Status: Ready" & vbCrLf strMessage = strMessage & "Free space: " & objDiskDrive.FreeSpace strMessage = strMessage & vbCrLf & vbCrLf Else
' Otherwise, just add the status to the message strMessage = strMessage & "Status: Not Ready" & vbCrLf & vbCrLf End If Next
' Display the message Wscript.Echo strMessage
|
This script creates a FileSystemObject and then uses its Drives property to return the system’s collection of disk drives. Then a For Each...Next
loop runs through the collection, gathering the drive letter, the
status, and, if the disk is ready, the free space. It then displays the
drive data as shown in Figure 2.